package cn.xiangxiang.service.impl;

import cn.xiangxiang.CacheKey;
import cn.xiangxiang.mapper.goods.CategoryMapper;
import cn.xiangxiang.pojo.goods.Category;
import cn.xiangxiang.service.goods.CategoryService;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CategoryService.class)
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Page<Category> findConditionPage(Integer startpage, Integer pagesize, Category category) {
        QueryWrapper<Category> wrapper = getCondition(category);
        Page<Category> page = new Page<>(startpage, pagesize);
        Page<Category> categoryPage = page(page, wrapper);
        return categoryPage;
    }

    @Override
    public void delById(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("parent_id",id);
        List<Category> categoryList = categoryMapper.selectByMap(map);
        if(categoryList.size()>0){
            throw  new RuntimeException("该类别下存在子类，不能删除");
        }
        removeById(id);
    }

    public void saveToRedis(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("is_show","1");
        List<Category> categoryList = listByMap(map);
         // 分类号的导航
        List<Map<String, Object>> list = findCategoryByParentId(categoryList, 0);
        redisTemplate.boundValueOps(CacheKey.CATEGORY_MENU).set(list);
    }


    /*查询导航*/
    public List<Map<String, Object>> findCategoryTree(){
        List<Map<String, Object>> redisCategoryList = (List<Map<String, Object>>)redisTemplate.boundValueOps(CacheKey.CATEGORY_MENU).get();
        if( redisCategoryList == null ){
            HashMap<String, Object> map = new HashMap<>();
            map.put("is_show","1");
            List<Category> categoryList = listByMap(map);
            List<Map<String, Object>> mysqlCategory = findCategoryByParentId(categoryList, 0);
            redisTemplate.boundValueOps(CacheKey.CATEGORY_MENU).set(mysqlCategory);
            return mysqlCategory;
        }
        return redisCategoryList;
    }

    @Override
    public List<Map<String, Object>> findCategoryMenu() {
        List<Category> categoryList = list();
        return findCategoryByParentId(categoryList,0);
    }

    private List<Map<String,Object>> findCategoryByParentId(List<Category> categoryList,Integer id){
        ArrayList<Map<String, Object>> rootList = new ArrayList<>();
        /*for (Category category : categoryList) {
            if(category.getParentId().equals(id)){
                HashMap<String, Object> map = new HashMap<>();
                map.put("value",category.getId());
                map.put("label",category.getName());
                List<Map<String, Object>> childrenList = findCategoryByParentId(categoryList, category.getId());
                // []
                if(childrenList.size()>0){
                    map.put("children",childrenList);
                }
                rootList.add(map);
            }
        }*/
        categoryList.forEach((category)->{
            if(category.getParentId().equals(id)){
                HashMap<String, Object> map = new HashMap<>();
                map.put("value",category.getId());
                map.put("label",category.getName());
                List<Map<String, Object>> childrenList = findCategoryByParentId(categoryList, category.getId());
                // []
                if(childrenList.size()>0){
                    map.put("children",childrenList);
                }
                rootList.add(map);
            }
        });
        return rootList;
    }

    private QueryWrapper<Category> getCondition(Category category){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if(category!=null){
            if(category.getName()!=null && !"".equals(category.getName().trim())){
                wrapper.like("name",category.getName());
            }
            if(category.getIsShow()!=null){
                wrapper.eq("is_show",category.getIsShow());
            }
            if(category.getParentId()!=null){
                wrapper.eq("parent_id",category.getParentId());
            }
        }
        return wrapper;
    }
}
